//
//  SecrectViewController.swift
//  RongYiHua
//
//  Created by 卢小艳 on 2025/7/14.
//

import Foundation
import UIKit

class JQFXHSecrectSettingViewController: HJYJBaseViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "设置密码"
        self.view.backgroundColor = UIColor.jqfxhBigViewBgccGray
        self.view.addSubview(jqfxhContentBgttView)
        jqfxhContentBgttView.snp.makeConstraints { make in
            make.left.right.equalTo(self.view)
            make.height.equalTo(110)
        }
        self.view.addSubview(jqfxhSecrectwwTf)
        jqfxhSecrectwwTf.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(20)
            make.right.equalTo(self.view).offset(-20)
            make.top.equalTo(self.view).offset(10)
            make.height.equalTo(40)
        }
        
        self.view.addSubview(jqfxhSecrectxxAgainTf)
        jqfxhSecrectxxAgainTf.snp.makeConstraints { make in
            make.left.right.equalTo(jqfxhSecrectwwTf)
            make.top.equalTo(jqfxhSecrectwwTf.snp.bottom).offset(10)
            make.height.equalTo(jqfxhSecrectwwTf)
        }
        
        self.view.addSubview(jqfxhtTueyyButton)
        jqfxhtTueyyButton.snp.makeConstraints { make in
            make.left.right.equalTo(jqfxhSecrectwwTf)
            make.top.equalTo(jqfxhContentBgttView.snp.bottom).offset(30)
            make.height.equalTo(40)
        }
        
        self.view.addSubview(jqfxhLinevvView)
        jqfxhLinevvView.snp.makeConstraints { make in
            make.left.right.equalTo(jqfxhContentBgttView)
            make.top.equalTo(jqfxhSecrectwwTf.snp.bottom).offset(5)
            make.height.equalTo(0.6)
        }
        
    }
    
    @objc func jqfxhDoneTapped() {
        view.endEditing(true) // 收起键盘
    }
    
    
    
    lazy var jqfxhContentBgttView : UIView = {
        let jqfxhContentBgttView = UIView()
        jqfxhContentBgttView.backgroundColor = UIColor.white
        return jqfxhContentBgttView
    }()
    
    
    @objc func jqfxhSetBtnaaClick() {
        
        var password = ""
        
        if let topCode = self.jqfxhSecrectwwTf.text {
            if let bottomCode = self.jqfxhSecrectxxAgainTf.text {
                if topCode.isEmpty {
                    JQFXHHUD.jqfxhShowxxText("请输入密码")
                    return
                }else if bottomCode.isEmpty {
                    JQFXHHUD.jqfxhShowxxText("请输入确认密码")
                    return
                }else if topCode.count < 4 {
                    JQFXHHUD.jqfxhShowxxText("密码至少需要4个字符")
                    return
                }else if topCode != bottomCode {
                    JQFXHHUD.jqfxhShowxxText("密码不一致")
                    return
                }
                password = topCode
            }else {
                return
            }
        }else {
            return
        }
        
        let prozzvider = JQFXHNetworkManager.shared.provider
        JQFXHNetworkManager.shared.jqfxhRequest(provider: prozzvider, target: .setPassjjword(password: password)) { result in
            JQFXHHUD.jqfxhHttidden()
            switch result {
            case.success(_):
                JQFXHHUD.jqfxhShowvvSuccess("密码设置成功")
                self.navigationController?.popViewController(animated: true)
            case .failure(let error):
                print(error)

            }
        }
        
        
    }
    lazy var jqfxhLinevvView : UIView = {
        let jqfxhLinevvView = UIView()
        jqfxhLinevvView.backgroundColor = UIColor.jqfxhLineyyGray
        return jqfxhLinevvView
    }()
    
    lazy var jqfxhSecrectwwTf : UITextField = {
        let jqfxhSecrectwwTf = UITextField()
        jqfxhSecrectwwTf.font = UIFont.jqfxhRegular14
        jqfxhSecrectwwTf.placeholder = "请输入密码"
        jqfxhSecrectwwTf.isSecureTextEntry = true

        jqfxhSecrectwwTf.jqfxhAddDoneButtoneeOnKeyboard(target: self, action: #selector(jqfxhDoneTapped))
        return jqfxhSecrectwwTf
    }()
    
    lazy var jqfxhSecrectxxAgainTf : UITextField = {
        let jqfxhSecrectwwTf = UITextField()
        jqfxhSecrectwwTf.font = UIFont.jqfxhRegular14
        jqfxhSecrectwwTf.placeholder = "请再次输入密码"
        jqfxhSecrectwwTf.isSecureTextEntry = true
        jqfxhSecrectwwTf.jqfxhAddDoneButtoneeOnKeyboard(target: self, action: #selector(jqfxhDoneTapped))
        return jqfxhSecrectwwTf
    }()
    
    
    lazy var jqfxhtTueyyButton: UIButton = {
        let jqfxhtTueyyButton = UIButton(type: .system)
        jqfxhtTueyyButton.setTitle("设置密码", for: .normal)
        jqfxhtTueyyButton.backgroundColor = UIColor.jqfxhThemexxRed
        jqfxhtTueyyButton.setTitleColor(.white, for: .normal)
        jqfxhtTueyyButton.layer.cornerRadius = 8
        jqfxhtTueyyButton.addTarget(self, action: #selector(jqfxhSetBtnaaClick), for: .touchUpInside)
        return jqfxhtTueyyButton
    }()
    
}
